/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WebService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javaSE.DBConnection;
import javaSE.UsersBean;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author Rui
 */
@WebService()
public class UserService {
  /*
     * Web service operation
     * @param DBConn
     * @param UserName
     * @param UserPassword
     * @param Email
     * @param UserID
     * @return
     * @throws SQLException
     */
    @WebMethod(operationName = "Add")
    public String Add(
            /*(@WebParam(name = "DBConn")
    DBConnection DBConn,*/
             @WebParam(name = "UserName")
    String UserName, @WebParam(name = "UserPassword")
    String UserPassword, @WebParam(name = "Email")
    String Email)  {
        DBConnection DBConn = new DBConnection();

        String sqlStatement="Insert into users(UserName,UserPassword,Email)";
        sqlStatement=sqlStatement+"values('"+UserName+"','"+UserPassword+"','";
        sqlStatement=sqlStatement+Email+"')";
        try {
            DBConn.executeUpdate(sqlStatement);

        } catch (SQLException ex) {
            Logger.getLogger(UserService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "succeed";
    }

    /**
     * Web service operation
      */
    @WebMethod(operationName = "Search")
    public ArrayList<UsersBean> Search() {
         ResultSet result = null;
            ArrayList<UsersBean> users = new ArrayList<UsersBean>();
UsersBean user =null;
            DBConnection DBConn = new DBConnection();
        try {

            String sqlStatement = "select * from users" ;
            try {
                result = DBConn.executeQuery(sqlStatement);
            } catch (SQLException ex) {
                Logger.getLogger(UserService.class.getName()).log(Level.SEVERE, null, ex);
            }
            while (result.next()) {
                 user = new UsersBean();
                 user.setUserName(result.getString("UserName"));
                 user.setPassword(result.getString("UserPassword"));
                 user.setEmail(result.getString("Email"));
                 users.add(user);
            }

        } catch (SQLException ex) {
            Logger.getLogger(UserService.class.getName()).log(Level.SEVERE, null, ex);
        }

           return users;

    }
}
